﻿// This software is the confidential and proprietary information of NetBay.
// Such confidential information should not be disclosed.
// NetBay copyright(c) 2012

#region

using NetBay.Core.BusinessEntities.Workflow;
using NetBay.Core.Log;
using Rio.Flow.Business.Repository;

#endregion

namespace Rio.Workflow.Runtime.Extentions
{
    public static class WorkflowInstanceExtention
    {
        #region Class Methods

        /// <summary>
        /// Changes the state of the document.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="newState">The new state.</param>
        /// <param name="documentId">The document id.</param>
        /// <param name="userId">The user id.</param>
        /// <param name="rguId">The rgu id.</param>
        /// <param name="actionOrConditionId">The action or condition id.</param>
        /// <param name="comment">The comment.</param>
        /// <returns></returns>
        public static bool ChangeDocumentState(this WorkflowInstance instance, WorkflowState newState, long documentId,
                                               long? userId, long? rguId, long actionOrConditionId, string comment = "")
        {
            return WorkflowInstanceRepository.UpdateDocumentStateById(instance.Identifier, documentId, newState, rguId,
                                                                      userId, actionOrConditionId, comment);
        }

        /// <summary>
        /// Changes the state of the documents.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="newState">The new state.</param>
        /// <param name="userId">The user id.</param>
        /// <param name="rguId">The rgu id.</param>
        /// <param name="actionOrConditionId">The action or condition id.</param>
        /// <param name="comment">The comment.</param>
        /// <returns></returns>
        public static bool ChangeDocumentsState(this WorkflowInstance instance, WorkflowState newState, long? userId,
                                                long? rguId, long actionOrConditionId, string comment = "")
        {
            return WorkflowInstanceRepository.UpdateDocumentStateByInst(instance.Identifier, newState, rguId, userId,
                                                                        actionOrConditionId, comment);
        }

        /// <summary>
        /// Changes the state of the instance.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="newState">The new state.</param>
        /// <param name="userId">The user id.</param>
        /// <param name="rguId">The rgu id.</param>
        /// <param name="isEndState">End state of the is.</param>
        /// <returns></returns>
        public static bool ChangeInstanceState(this WorkflowInstance instance, WorkflowState newState, long? userId,
                                               long? rguId, bool isEndState = false)
        {
            bool result = WorkflowInstanceRepository.UpdateInstance(instance.Identifier, newState, userId, rguId,
                                                                    isEndState);
            LogManager.Instance().LogDebugFormat(Message.NewWorkflowStateLog, instance.Identifier, newState.ToString());
            if (result)
            {
                instance.State = newState;
                instance.IsEnded = isEndState;
            }
            return result;
        }

        /// <summary>
        /// Changes the instance state with no user.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="newState">The new state.</param>
        /// <returns></returns>
        public static bool ChangeInstanceStateWithNoUser(this WorkflowInstance instance, WorkflowState newState)
        {
            bool result = WorkflowInstanceRepository.UpdateInstance(instance.Identifier, newState, null, null);
            if (result)
            {
                instance.State = newState;
            }
            LogManager.Instance().LogDebugFormat(Message.NewWorkflowStateLog, instance.Identifier, newState.ToString());
            return result;
        }

        /// <summary>
        /// Changes the state of the user document.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="newState">The new state.</param>
        /// <param name="documentId">The document id.</param>
        /// <returns></returns>
        public static bool ChangeUserDocumentState(this WorkflowInstance instance, WorkflowState newState,
                                                   long documentId)
        {
            return WorkflowInstanceRepository.UpdateUserInstanceDocState(instance.InstanceUserId, documentId, newState);
        }

        /// <summary>
        /// Changes the state of the documents.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="newState">The new state.</param>
        /// <returns></returns>
        public static bool ChangeUserDocumentsState(this WorkflowInstance instance, WorkflowState newState)
        {
            return WorkflowInstanceRepository.UpdateUserInstanceDocState(instance.InstanceUserId, null, newState);
        }

        /// <summary>
        /// Restarts the workflow instance.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="newReceivers">The new receivers.</param>
        /// <returns></returns>
        public static bool RestartWorkflowInstance(this WorkflowInstance instance, string newReceivers)
        {
            return WorkflowInstanceRepository.RestartWorkflowInstance(instance.Identifier, newReceivers);
        }

        /// <summary>
        /// Sets the instance to ended.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="isEnded">The is ended.</param>
        /// <param name="userId">The user id.</param>
        /// <param name="rguId">The rgu id.</param>
        /// <returns></returns>
        public static bool SetInstanceToEnded(this WorkflowInstance instance, bool isEnded, long? userId, long? rguId)
        {
            bool result = WorkflowInstanceRepository.UpdateInstanceToEnded(instance.Identifier, isEnded, rguId, userId);
            if (result)
            {
                instance.IsEnded = isEnded;
            }
            return result;
        }

        #endregion
    }
}